package com.cyzy.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cyzy.dto.Result;
import com.cyzy.mapper.CouponSeckillMapper;
import com.cyzy.pojo.CouponSeckill;
import com.cyzy.pojo.ErrorProblem;
import com.cyzy.service.CouponSeckillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ll
 * @version 1.0
 * @description: TODO
 * @date 2025/5/26 下午6:58
 */
@RestController
@RequestMapping("/sekill")
public class CouponSeckillController {
    @Autowired
    private CouponSeckillService couponSeckillService;
    @Autowired
    private CouponSeckillMapper couponSeckillMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //分页查询
    @GetMapping("select")
    public Result getKillList(CouponSeckill couponSeckill){
        PageHelper.startPage(couponSeckill.getPage(),couponSeckill.getPageSize());
        List<CouponSeckill> couponSeckills = couponSeckillService.selectKillList(couponSeckill);
        if(couponSeckills == null){
            return Result.error().message("查询失败");
        }
        PageInfo<CouponSeckill> pageInfo = new PageInfo<>(couponSeckills);
        return Result.ok().data("killList",pageInfo).data("count",pageInfo.getTotal());
    }
    //批量新增
    @PostMapping("batchAdd")
    public Result batchAdd(@RequestBody List<CouponSeckill> couponSeckills){
        System.out.println(couponSeckills+"-------------------40");
        boolean b = couponSeckillService.saveBatch(couponSeckills);
        if(!b){
            return Result.error().message("失败");
        }
        // 获取刚刚插入的记录的ID列表
        List<Long> insertedIds = couponSeckills.stream()
                .map(CouponSeckill::getSeckillId)
                .collect(Collectors.toList());

        // 查询新增的记录
        QueryWrapper<CouponSeckill> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("seckill_id", insertedIds); // 使用in条件查询刚刚插入的记录
        List<CouponSeckill> newlyInserted = couponSeckillMapper.selectList(queryWrapper);
        System.out.println(newlyInserted+"=========================");
        //超卖
        for (CouponSeckill seckill : newlyInserted) {
            String keyId = "seckill-" + seckill.getSeckillId();//命名规范：功能 + id
            redisTemplate.delete(keyId);
            //存入radis
            for (int i = 0; i < seckill.getSeckillNum(); i++) {
                redisTemplate.opsForList().leftPush(keyId,keyId+"-"+i);
            }
        }
        return Result.ok().data("newlyInserted", newlyInserted).message("新增成功");
    }
    //修改记录
    @PutMapping("updateSeckill")
    public Result update(@RequestBody CouponSeckill couponSeckill){
        boolean update = couponSeckillService.updateById(couponSeckill);
        if(update){
            return Result.ok().message("编辑成功");
        }
        return Result.error().message("失败");
    }
    //批量修改状态
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam List<Integer> seckillIds){
        LambdaUpdateWrapper<CouponSeckill> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(CouponSeckill::getSeckillStatus,1)
                .in(CouponSeckill::getSeckillId,seckillIds);
        boolean update = couponSeckillService.update(updateWrapper);
        if(update){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("失败");
    }
    //删除一个
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("seckillId") Integer seckillId){
        System.out.println(seckillId);
        LambdaUpdateWrapper<CouponSeckill> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(CouponSeckill::getSeckillStatus,1)
                .in(CouponSeckill::getSeckillId,seckillId);
        // 2. 执行更新
        boolean update = couponSeckillService.update(updateWrapper);
        if(update){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("失败");
    }

}
